package com.gcsoftware.web.mapper.elasticsearch;

import com.gcsoftware.utils.elasticsearch.StringUtils;
import com.gcsoftware.web.service.elasticsearch.impl.TemplateRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import static com.gcsoftware.configs.AppConstants.Elasticsearch.*;
import static com.gcsoftware.configs.AppConstants.Sys.SYS_MEASURE_DAY;
import static com.gcsoftware.configs.AppConstants.Sys.SYS_MEASURE_SECOND;

/**
 * 首页
 * @author czp
 * data : 2018-04-11
 */
@Repository
public class HomeDao<T> {
    @Autowired
    private TemplateRepository<T> template;

    @Value("${elasticsearch.index}")
    private String index;

    public Object apiCountLine(String range) {
        StringBuffer aggs = new StringBuffer();
        String query = StringUtils.queryGroupRange(range, null, SYS_MEASURE_DAY.getValue());
        aggs.append("{\"")
            .append(ES_APP_NAME_GROUPS.getValue())
            .append("\":{\"terms\":{\"field\":\"")
            .append(ES_APP_NAME.getValue())
            .append(".keyword\",\"size\":10000,\"order\":{\"_count\":\"desc\"}},\"aggs\":{\"")
            .append(ES_TIME_GROUPS.getValue())
            .append("\":{\"date_histogram\":{\"field\":\"")
            .append(ES_CREATE_TIME.getValue())
            .append("\",\"interval\":\"1d\",\"format\":\"yyyy-MM-dd\",\"min_doc_count\":1}}}}}");
        return template.queryDslGroup(index, null, query, aggs.toString(), ES_APP_NAME_GROUPS.getValue());
    }

    public T sensitiveItems(String range) {
        StringBuffer aggs = new StringBuffer();
        String query = StringUtils.queryGroupRange(range, null, SYS_MEASURE_SECOND.getValue());
        aggs.append("{\"").append(ES_API_NAME_GROUPS.getValue())
            .append("\":{\"terms\":{\"field\":\"").append(ES_API_NAME.getValue())
            .append(".keyword\",\"size\":1000,\"order\":{\"_count\":\"desc\"}}}}");
        return template.queryDslGroup(index, null, query, aggs.toString(), ES_API_NAME_GROUPS.getValue());
    }

    public T apiCountBar(String range, String appName, String apiName) {
        StringBuffer aggs = new StringBuffer();
        StringBuffer query = new StringBuffer();
        query.append("{\"bool\":{\"filter\":[")
             .append(StringUtils.queryPageRange(range, SYS_MEASURE_DAY.getValue()))
             .append("],\"must\":[")
             .append(StringUtils.queryTerms(apiName, appName))
             .append("]}}");
        aggs.append("{\"")
            .append(ES_APP_NAME_GROUPS.getValue())
            .append("\":{\"terms\":{\"field\":\"")
            .append(ES_APP_NAME.getValue())
            .append(".keyword\",\"size\":10000,\"order\":{\"_count\":\"desc\"}},\"aggs\":{\"")
            .append(ES_API_NAME_GROUPS.getValue())
            .append("\":{\"terms\":{\"field\":\"")
            .append(ES_API_NAME.getValue())
            .append(".keyword\",\"size\":\"10000\"}}}}}");
        return template.queryDslGroup(index, null, query.toString(), aggs.toString(), ES_APP_NAME_GROUPS.getValue());
    }

    public long interfaceUsage(String range) {
        return template.fieldCount(index, null, "{}");
    }
}
